# -*- coding: utf-8 -*-
import scrapy
from ..items import WineSearcherItem


# 1.2 写爬虫爬取国家信息列表, 通过 国际信息创建接口, 把数据存入mysql中
class WineSpider(scrapy.Spider):
    name = 'wine'
    start_urls = ['https://www.wine-searcher.com/regions']

    def parse(self, response):
        all_li = response.xpath("//div[@id='navigation']/ul[@class='top-level'][2]/li/a")
        c_id = 0
        for li in all_li:
            c_name = li.xpath("./text()")[0].extract()
            c_href = 'https://www.wine-searcher.com' + li.xpath("./@href")[0].extract()

            c_id += 1
            # item = WineSearcherItem()
            # item['c_name'] = c_name
            # item['c_href'] = c_href
            # yield item

            yield scrapy.Request(url=c_href, callback=self.parse_page, meta={'c_id': c_id})

    def parse_page(self, response):
        p_id = 0
        for page in range(1, 500, 25):
            c_id = response.meta['c_id']
            p_href = 'https://www.wine-searcher.com/regions-argentina/' + str(page)

            p_id += 1
            # item = WineSearcherItem()
            # item['p_href'] = p_href
            # item['c_id'] = c_id
            # yield item

            yield scrapy.Request(url=p_href, callback=self.parse_wine, meta={'p_id': p_id})

    def parse_wine(self, response):
        p_id = response.meta['p_id']
        all_tr = response.xpath("//div[@id='winesortlist']//table[@class='nltbl tab-zero']//tr[@class='wlrwdt']")
        for tr in all_tr:
            w_href = tr.xpath(".//a[@class='wine-suggest-link']/@href")
            yield scrapy.Request(url=w_href, callback=self.parse_info, meta={'p_id': p_id})

    def parse_info(self, response):
        p_id = response.meta['p_id']
        region = response.xpath("//div[@class='dtc'][5]/span[@class='dtlbl sidepanel-text']/a[1]/font/font")[
            0].extract()
        ex_tax = response.xpath("//div[@class='dtc'][1]/span[@class='dtlbl sidepanel-text']/b")[0].extract()
        producer = response.xpath("//div[@class='dtc'][4]/span[@class='dtlbl sidepanel-text']/a")[0].extract()
        grape = response.xpath("//div[@class='dtc'][6]/span[@class='dtlbl sidepanel-text']/a")[0].extract()
        awards = response.xpath(
            "string(//div[@class='dtc noborderbtm']/span[@class='dtlbl sidepanel-text']/div[@class='smallish'])").extract()
        notes = response.xpath("string(//div[@class='dtc'][10]/span[@class='dtlbl sidepanel-text']/div)").extract()

        item = WineSearcherItem()
        item['region'] = region
        item['ex_tax'] = ex_tax
        item['producer'] = producer
        item['grape'] = grape
        item['awards'] = awards
        item['notes'] = notes
        item['p_id'] = p_id
        yield item
